package com.rickpan.controller;

import com.rickpan.dto.common.ApiResponse;
import com.rickpan.dto.response.WorkCategoryResponse;
import com.rickpan.service.WorkCategoryService;
import com.rickpan.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工作分类控制器
 */
@RestController
@RequestMapping("/api/work-reports/categories")
@Tag(name = "工作分类管理", description = "工作分类相关接口")
public class WorkCategoryController {

    private static final Logger logger = LoggerFactory.getLogger(WorkCategoryController.class);

    @Autowired
    private WorkCategoryService workCategoryService;



    /**
     * 获取用户的所有分类（包括系统分类）
     */
    @GetMapping
    @Operation(summary = "获取用户分类", description = "获取用户的所有分类，包括系统分类和用户自定义分类")
    public ApiResponse<List<WorkCategoryResponse>> getUserCategories(HttpServletRequest request) {
        Long userId = SecurityUtils.getCurrentUserId();
        logger.info("获取用户分类，用户ID: {}", userId);

        List<WorkCategoryResponse> categories = workCategoryService.getUserCategories(userId);
        return ApiResponse.success(categories, "获取分类列表成功");
    }

    /**
     * 获取系统分类
     */
    @GetMapping("/system")
    @Operation(summary = "获取系统分类", description = "获取所有系统预定义分类")
    public ApiResponse<List<WorkCategoryResponse>> getSystemCategories() {
        logger.info("获取系统分类");

        List<WorkCategoryResponse> categories = workCategoryService.getSystemCategories();
        return ApiResponse.success(categories, "获取系统分类成功");
    }

    /**
     * 创建用户自定义分类
     */
    @PostMapping
    @Operation(summary = "创建分类", description = "创建用户自定义分类")
    public ApiResponse<WorkCategoryResponse> createCategory(
            HttpServletRequest request,
            @Parameter(description = "分类名称", required = true) @RequestParam String name,
            @Parameter(description = "分类描述") @RequestParam(required = false) String description,
            @Parameter(description = "分类颜色") @RequestParam(defaultValue = "#409EFF") String color,
            @Parameter(description = "分类图标") @RequestParam(defaultValue = "folder") String icon) {
        
        Long userId = SecurityUtils.getCurrentUserId();
        logger.info("创建用户分类，用户ID: {}, 分类名称: {}", userId, name);

        WorkCategoryResponse category = workCategoryService.createUserCategory(userId, name, description, color, icon);
        return ApiResponse.success(category, "分类创建成功");
    }

    /**
     * 更新用户自定义分类
     */
    @PutMapping("/{categoryId}")
    @Operation(summary = "更新分类", description = "更新用户自定义分类")
    public ApiResponse<WorkCategoryResponse> updateCategory(
            HttpServletRequest request,
            @Parameter(description = "分类ID", required = true) @PathVariable Long categoryId,
            @Parameter(description = "分类名称") @RequestParam(required = false) String name,
            @Parameter(description = "分类描述") @RequestParam(required = false) String description,
            @Parameter(description = "分类颜色") @RequestParam(required = false) String color,
            @Parameter(description = "分类图标") @RequestParam(required = false) String icon) {
        
        Long userId = SecurityUtils.getCurrentUserId();
        logger.info("更新用户分类，用户ID: {}, 分类ID: {}", userId, categoryId);

        WorkCategoryResponse category = workCategoryService.updateUserCategory(userId, categoryId, name, description, color, icon);
        return ApiResponse.success(category, "分类更新成功");
    }

    /**
     * 删除用户自定义分类
     */
    @DeleteMapping("/{categoryId}")
    @Operation(summary = "删除分类", description = "删除用户自定义分类")
    public ApiResponse<Void> deleteCategory(
            HttpServletRequest request,
            @Parameter(description = "分类ID", required = true) @PathVariable Long categoryId) {
        
        Long userId = SecurityUtils.getCurrentUserId();
        logger.info("删除用户分类，用户ID: {}, 分类ID: {}", userId, categoryId);

        workCategoryService.deleteUserCategory(userId, categoryId);
        return ApiResponse.<Void>success("分类删除成功");
    }

    /**
     * 获取分类详情
     */
    @GetMapping("/{categoryId}")
    @Operation(summary = "获取分类详情", description = "根据ID获取分类详情")
    public ApiResponse<WorkCategoryResponse> getCategoryById(
            @Parameter(description = "分类ID", required = true) @PathVariable Long categoryId) {
        
        logger.info("获取分类详情，分类ID: {}", categoryId);

        WorkCategoryResponse category = workCategoryService.getCategoryById(categoryId);
        return ApiResponse.success(category, "获取分类详情成功");
    }
}
